# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from odoo.exceptions import ValidationError, Warning

import sys
import os
from os import path
d = path.dirname(__file__)
parent_path = os.path.dirname(os.path.dirname(d))
sys.path.append(parent_path)
from celery_odoo import sms

# sys.path.append(os.path.join(parent_path,'celery_odoo'))
# import sms

class NoticeCategory(models.Model):
    """
    内、外部公告的类别
    """
    _name = "tyibs.base.notice.category"

    name = fields.Char(string="名称", size=100, required=True)
    remark = fields.Text(string="备注")


class Notice(models.Model):
    _name = "tyibs.base.notice"
    _rec_name = "title"

    title = fields.Char(string="标题", size=100, required=True)
    category_id = fields.Many2one("tyibs.base.notice.category", string="分类", required=True)
    notice_type = fields.Selection([('in', '内部'),('out', '外部')], string="公告类型", default="in")
    send_qs_company = fields.Many2one("tyibs.base.qs.company", string="发起单位", required=True)
    in_recevied_company_ids = fields.Many2many("tyibs.base.qs.company", "in_rece_company_notice_rel", "notice_id", "enterprise_id", string="接收单位")
    out_main_company_ids = fields.Many2many("tyibs.base.maintenance.company", "notice_maintenance_com_rel", "notice_id", "enterprise_id", string="维保单位")
    out_use_company_ids = fields.Many2many("tyibs.base.lift.use.company", "notice_use_com_rel", "notice_id", "enterprise_id", string="维保单位")

    content = fields.Html(string="公告内容")
    attachment_ids = fields.Many2many("ir.attachment", "notice_attachment_rel", "notice_id", "attachment_id", string="附件")
    send_time = fields.Datetime(string="发送时间", default=lambda self: fields.Datetime.now())

    @api.model
    def create(self, values):
        record = super(Notice, self).create(values)
        app_notice = {'title':values['title']}
        if values['in_recevied_company_ids']:
            app_notice['qs'] = values['in_recevied_company_ids'][0][2]
        if values['out_main_company_ids']:
            app_notice['main'] = values['out_main_company_ids'][0][2]
        if values['out_use_company_ids']:
            app_notice['use'] = values['out_use_company_ids'][0][2]
        # app 发送通知只有维保单位和检验单位
        if app_notice.has_key('main'):
            app_notice['id'] = record.id
            run_env = self.env['ir.config_parameter'].get_param('tyibs.runtime.environment')
            if run_env == "master":
                # 生产环境推送消息
                sms.send_notice.delay(app_notice)
        return record


class PublicNotice(models.Model):
    _name = "tyibs.base.public.notice"
    _rec_name = "title"

    title = fields.Char(string="标题", size=100, required=True)
    category_id = fields.Many2one("tyibs.base.notice.category", string="分类", required=True)
    content = fields.Html(string="公告内容")
    send_time = fields.Datetime(string="发送时间", default=lambda self: fields.Datetime.now()) 
    attachment_ids = fields.Many2many("ir.attachment", "notice_attachment_rel", "notice_id", "attachment_id", string="附件")
